package com.yq.cp.controller.basics;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.*;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.User;
import com.yq.cp.entity.yw.YwGdxx;
import com.yq.cp.entity.yw.YwZbglGlry;
import com.yq.cp.service.basics.*;
import com.yq.cp.service.permission.UserService;
import com.yq.cp.service.yw.YwGdxxService;
import com.yq.cp.service.yw.YwZbglGlryService;
import com.yq.cp.utils.DateTimeUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 巡检任务管理 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/inspectionTaskManagement")
public class InspectionTaskManagementController {
    @Resource
    InspectionTaskManagementService inspectionTaskManagementService;
    @Resource
    InspectionPointManagementExtendService inspectionPointManagementExtendService;
    @Resource
    InspectionItemManagementExtendService inspectionItemManagementExtendService;
    @Resource
    PersonnelRelationsService personnelRelationsService;
    @Resource
    YwGdxxService ywGdxxService;
    @Resource
    YwZbglGlryService ywZbglGlryService;
    @Resource
    OrganizationalStructureService organizationalStructureService;
    @Resource
    UserService userService;
    /**
     * 实时添加
     * @param vo
     * @return
     */
    @PostMapping("realAdd")
    public ApiResult realAdd(@RequestBody InspectionTaskManagement vo){
        inspectionTaskManagementService.realAdd(vo);
        return ApiResult.success();
    }

    /**
     * 领取
     * @param vo
     * @return
     */
    @PostMapping("lq")
    public ApiResult lq(@RequestBody InspectionTaskManagement vo){
        //todo
        User user = UserThreadLocal.get();
        vo.setState(1);
        vo.setReceivingid(user.getId());
        vo.setReceivingname(user.getNickname());
        vo.setReceivingtime(DateTimeUtil.dqsj());
        inspectionTaskManagementService.saveOrUpdate(vo);
        InspectionTaskManagement byId = inspectionTaskManagementService.getById(vo);
        return  ApiResult.success();
    }

    /**
     * 列表
     * @param vo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo vo){
        User user = UserThreadLocal.get();
        Page<InspectionTaskManagement> page = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<InspectionTaskManagement> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("plan_name",vo.getLike());
        wrapper.eq("state",vo.getState());
        wrapper.eq("site_id",vo.getSiteId());
        wrapper.orderByDesc("create_time");
        if (vo.getState()!=0)
            wrapper.eq("receivingid",user.getId());
        if (vo.getState()==0){
            List<PersonnelRelations> userId = personnelRelationsService.list(new QueryWrapper<PersonnelRelations>()
                    .eq("user_id", user.getId()));
            if (!userId.isEmpty()){
                List<Long> collect = userId.stream().map(PersonnelRelations::getOtherId).collect(Collectors.toList());
                wrapper.in("id",collect);
            }else
                wrapper.eq("id",-1);
        }
        page = inspectionTaskManagementService.page(page,wrapper);
        return  ApiResult.success(page);
    }

    /**
     * 详情
     * @param vo
     * @return
     */
    @PostMapping("detail")
    public ApiResult detail(@RequestBody InspectionTaskManagement vo){
        InspectionTaskManagement byId = inspectionTaskManagementService.getById(vo);
        OrganizationalStructure byId1 = organizationalStructureService.getById(byId.getSiteId());
        byId.setSiteName(byId1.getName());
        List<InspectionPointManagementExtend> pointManagementExtends = inspectionPointManagementExtendService.list(new QueryWrapper<InspectionPointManagementExtend>()
                .eq("plan_id", byId.getId()));
        for (InspectionPointManagementExtend pointManagementExtend : pointManagementExtends) {
            List<InspectionItemManagementExtend> list = inspectionItemManagementExtendService.list(new QueryWrapper<InspectionItemManagementExtend>()
                    .eq("point_id", pointManagementExtend.getId()));
            pointManagementExtend.setItemManagementExtends(list);
        }
        byId.setPointManagementExtends(pointManagementExtends);
        List<PersonnelRelations> list = personnelRelationsService.list(new QueryWrapper<PersonnelRelations>()
                .eq("other_id",byId.getId()));
        List<Long> collect = list.stream().map(PersonnelRelations::getUserId).collect(Collectors.toList());
        List<User> users = userService.listByIds(collect);
        byId.setUsers(users);
        return  ApiResult.success(byId);
    }

    /**
     * 执行
     * @param vo
     * @return
     */
    @PostMapping("execute")
    public ApiResult execute(@RequestBody List<InspectionPointManagementExtend> vo){
        for (InspectionPointManagementExtend inspectionPointManagementExtend : vo) {
            inspectionItemManagementExtendService.saveOrUpdateBatch(inspectionPointManagementExtend.getItemManagementExtends());
            inspectionPointManagementExtend.setState(1);
            inspectionPointManagementExtendService.saveOrUpdate(inspectionPointManagementExtend);
        }
        InspectionPointManagementExtend byId = inspectionPointManagementExtendService.getById(vo.get(0).getId());
        List<InspectionPointManagementExtend> extendList = inspectionPointManagementExtendService.list(new QueryWrapper<InspectionPointManagementExtend>()
                .eq("plan_id", byId.getPlanId()));
        Boolean b = true;
        for (InspectionPointManagementExtend extend : extendList) {
            if (extend.getState()==0) {
                b = false;
                break;
            }
        }
        if (b){
            InspectionTaskManagement management = inspectionTaskManagementService.getById(byId.getPlanId());
            management.setState(2);
            inspectionTaskManagementService.saveOrUpdate(management);
        }
        return  ApiResult.success();
    }

    /**
     * 提交问题
     */
    @PostMapping("question")
    public ApiResult question(@RequestBody InspectionTaskManagement vo){
        inspectionTaskManagementService.saveOrUpdate(vo);
        InspectionTaskManagement byId = inspectionTaskManagementService.getById(vo);
        //todo 创建一个工单
        YwGdxx ywGdxx = new YwGdxx();
        Long userId = UserThreadLocal.get().getId();
        ywGdxx.setSqrId(userId);
        ywGdxx.setFzrAuditState(0);
        ywGdxx.setZzAuditState(0);
        ywGdxx.setState(1);
        ywGdxx.setNzzt(0);
        ywGdxx.setHxzt(1);
        ywGdxx.setGdlxId(1L);
        ywGdxx.setWtms(vo.getQuestionContent());
        ywGdxx.setScwttp(vo.getQuestionPhoto());
        ywGdxxService.saveOrUpdate(ywGdxx);
        YwZbglGlry ywZbglGlry = new YwZbglGlry();
        ywZbglGlry.setUserId(byId.getHeadId());
        ywZbglGlry.setGlId(ywGdxx.getId());
        ywZbglGlry.setType(1);
        ywZbglGlry.setFl(4);
        ywZbglGlryService.saveOrUpdate(ywZbglGlry);
        List<PersonnelRelations> list = personnelRelationsService.list(new QueryWrapper<PersonnelRelations>()
                .eq("other_id", vo.getId()));
        for (PersonnelRelations personnelRelations : list) {
            ywZbglGlry.setUserId(personnelRelations.getUserId());
            ywZbglGlry.setGlId(ywGdxx.getId());
            ywZbglGlry.setType(3);
            ywZbglGlry.setFl(4);
            ywZbglGlry.setId(null);
            ywZbglGlryService.saveOrUpdate(ywZbglGlry);
        }
        return  ApiResult.success();
    }


}

